import { transferableCashing, getDistributionCenter } from "../../../api/cloudPointController";
import { getCustBaseById } from "../../../api/custBaseController";
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    integralDetails:'',   //积分详情
    dfDetail:{
      transferCustId:'888',
      amount: '1000',
    },
    cloudPointId:'',    //云积分id
    modalView: false,
    distributor: null,    //配送ID查到的详细信息
  },
  onLoad(options){
    let { id } = options
    this.getDistributionCenter()
    this.setData({
      cloudPointId: id
    })
  },
  /**
   * 配送中心
   */
  async getDistributionCenter(){
    let resolve = await getDistributionCenter({
      custId: wx.getStorageSync('custId')
    })
    if (resolve.success) {
      if (resolve.data==null) {
        this.setData({
          ['integralDetails.availableBalance']: 0
        })
      }else{
        this.setData({
          integralDetails: resolve.data,
        })
      }
    }
  },
  /**
   * 输入框
   */
  setInput(e){
    let { type,  } = e.currentTarget.dataset
    let { value } = e.detail
    this.setData({
      ['dfDetail.'+type] :value
    })
  },
  /**
   * 弹窗
   */
  async openDialog(){
    const self = this
    let resolve = await getCustBaseById({
      id: self.data.dfDetail.transferCustId
    })
    if (resolve.success) {
      if (resolve.data) {
        self.setData({
          modalView: true,
          distributor: resolve.data
        })
      }else{
        wx.showToast({
          title: '未找到该配送ID',
          icon: 'none'
        })
      }
    }else{
      wx.showToast({
        title: resolve.errorMessage,
        icon: 'none'
      })
    }
  },
  /**
   * 兑付
   */
  async extract(){
    let self = this
    let numberReg = /^[0-9]*$/

    if (self.data.integralDetails.availableBalance == 0) {
      wx.showToast({
        title: '当前未存在可用积分',
        icon: 'none',
      })
      return
    }

    if (self.data.dfDetail.transferCustId=='' || self.data.dfDetail.transferCustId==null) {
      wx.showToast({
        title: '请填写配送ID！',
        icon: 'none',
      })
      return
    }

    if (self.data.dfDetail.amount=='' || self.data.dfDetail.amount==null) {
      wx.showToast({
        title: '请填写积分！',
        icon: 'none',
      })
      return
    }

    if (!numberReg.test(self.data.dfDetail.amount)) {
      wx.showToast({
        title: '请填写积分！',
        icon: 'none',
      })
      return
    }

    if (self.data.dfDetail.amount > self.data.integralDetails.availableBalance) {
      wx.showToast({
        title: '您还没有这么多积分~',
        icon: 'none',
      })
      return
    }


    let resolve = await transferableCashing({
      cloudPointId: self.data.cloudPointId,
      ...self.data.dfDetail
    })
    if (resolve.success) {
      wx.showToast({
        title: '兑付成功！',
        mask: true
      })
      setTimeout(() => {
        self.getDistributionCenter()
        self.setData({
          ['dfDetail.transferCustId']:'',
          ['dfDetail.amount']:'',
        })
      }, 1500);
    }else{
      wx.showToast({
        title: '兑付失败',
        icon: 'none'
      })
    }
  },
  /**
   * 取消
   */
  handlerNo(){
    this.setData({
      modalView: false
    })
  },
  /**
   * 确定
   */
  handlerOk(){
    this.setData({
      modalView: false
    })
  },
})